package com.application.chiettinhdienluc.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.application.chiettinhdienluc.app.MainApplication;
import com.application.chiettinhdienluc.data.SharedPreferencesData;
import com.application.chiettinhdienluc.model.TramModel;

public class TramDataSource {
	// Database fields
	private final MySQLiteHelper dbHelper;

	public static final String TABLE_TRAM = "D_TRAM";
	public static final String COLUMN_MA_DVIQLY = "MA_DVIQLY";
	public static final String COLUMN_MA_TRAM = "MA_TRAM";
	public static final String COLUMN_TEN_TRAM = "TEN_TRAM";
	public static final String COLUMN_DINH_DANH = "DINH_DANH";
	public static final String COLUMN_LOAI_TRAM = "LOAI_TRAM";
	public static final String COLUMN_CSUAT_TRAM = "CSUAT_TRAM";
	public static final String COLUMN_MA_CAPDA = "MA_CAPDA";
	public static final String COLUMN_MA_CAPDA_RA = "MA_CAPDA_RA";
	public static final String COLUMN_MA_TO = "MA_TO";
	public static final String COLUMN_TINH_TRANG = "TINH_TRANG";
	public static final String COLUMN_HS_PHU = "HS_PHU";

	private final static String TAG = TramDataSource.class.getSimpleName();

	public TramDataSource() {
		dbHelper = MySQLiteHelper.getInstance();
	}
	
	
	public boolean checkTramExist(String maTram, String maDonVi) {

		boolean existed = false;

		Cursor c = null;
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				c = dbHelper.getDb().rawQuery("select rowid from "
						+ TABLE_TRAM + " where MA_TRAM = " 
						+ "'" + maTram  + "'" + " and " + "MA_DVIQLY = '" + maDonVi + "'" , null);		

				if (c != null && c.getCount() > 0){
					existed = true;			
				}
			}	

			
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally{
			if(c!=null)
			{
				c.close();
				c = null;
			}
		}

		return existed;	
	}



	public long insert(TramModel tram) {

		long row_id = -1;
		
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				ContentValues cv = new ContentValues();
				
				cv.put(COLUMN_MA_DVIQLY, tram.getMaDonViQuanLy());
				cv.put(COLUMN_MA_TRAM, tram.getMaTram());
				cv.put(COLUMN_TEN_TRAM, tram.getTenTram());
				cv.put(COLUMN_DINH_DANH, tram.getDinhDanh());
				cv.put(COLUMN_LOAI_TRAM, tram.getLoaiTram());
				cv.put(COLUMN_CSUAT_TRAM, tram.getCongSuatTram());
				cv.put(COLUMN_MA_CAPDA, tram.getMaCapDA());
				cv.put(COLUMN_MA_CAPDA_RA, tram.getMaCapDaRa());
				cv.put(COLUMN_MA_TO, tram.getMaTo());
				cv.put(COLUMN_TINH_TRANG, tram.getTinhTrang());
				cv.put(COLUMN_HS_PHU, tram.getHoSoPhu());
		
				row_id = dbHelper.getDb().insert(TABLE_TRAM, null, cv);
			}	
		} catch (Exception e) {
		}
		finally{
		}

		return row_id;
	}

	public int update(TramModel tram) {
		int numRowEffect = 0;
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				ContentValues cv = new ContentValues();
				
				cv.put(COLUMN_MA_DVIQLY, tram.getMaDonViQuanLy());
				cv.put(COLUMN_MA_TRAM, tram.getMaTram());
				cv.put(COLUMN_TEN_TRAM, tram.getTenTram());
				cv.put(COLUMN_DINH_DANH, tram.getDinhDanh());
				cv.put(COLUMN_LOAI_TRAM, tram.getLoaiTram());
				cv.put(COLUMN_CSUAT_TRAM, tram.getCongSuatTram());
				cv.put(COLUMN_MA_CAPDA, tram.getMaCapDA());
				cv.put(COLUMN_MA_CAPDA_RA, tram.getMaCapDaRa());
				cv.put(COLUMN_MA_TO, tram.getMaTo());
				cv.put(COLUMN_TINH_TRANG, tram.getTinhTrang());
				cv.put(COLUMN_HS_PHU, tram.getHoSoPhu());
		
				String whereClause = "MA_DVIQLY" + "=?" + " and " + "MA_TRAM" + "=?";
				String[] whereArgs = new String[] { tram.getMaDonViQuanLy(), tram.getMaTram()};
		
				numRowEffect = dbHelper.getDb().update(TABLE_TRAM, cv, whereClause, whereArgs);
			}	
		} catch (Exception e) {
		}
		finally{
		}
		
		return numRowEffect;
	}

	public ArrayList<TramModel> loadTram() {
		ArrayList<TramModel> listtramModel = new ArrayList<TramModel>();
		
		Cursor c = null;
		
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				c = dbHelper.getDb().rawQuery("select * from " + TramModel.TRAM_TABLE_NAME + " where " + COLUMN_MA_DVIQLY + " = ?", new String[]{SharedPreferencesData.getMaDonViQuanLy(MainApplication.getAppContext())});
				
				if (c != null && c.getCount() > 0) {
					int colId_MaDVQL = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[0]);
					int colId_Matram = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[1]);
					int colId_TenTram = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[2]);
					int colId_DinhDanh = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[3]);
					int colId_LoaiTram = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[4]);
					int colId_CSuatTram = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[5]);
					int colId_MaCapDA = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[6]);
					int colId_MaCapDARa = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[7]);
					int colId_MaTo = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[8]);
					int colId_TinhTrang = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[9]);
					int colId_HSPhu = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[10]);
		
					c.moveToFirst();
					do {
						try {
							String maDVQL = c.getString(colId_MaDVQL);
							String maTram = c.getString(colId_Matram);
							String tenTram = c.getString(colId_TenTram);
							String dinhDanh = c.getString(colId_DinhDanh);		
							String loaiTram = c.getString(colId_LoaiTram);	
							int cSuatTram = c.getInt(colId_CSuatTram);	
							String maCapDA = c.getString(colId_MaCapDA);	
							String maCapDARa = c.getString(colId_MaCapDARa);	
							String maTo = c.getString(colId_MaTo);	
							int tinhTrang = c.getInt(colId_TinhTrang);	
							int hSPhu = c.getInt(colId_HSPhu);	
		
							TramModel tram = new TramModel();
		
							tram.setMaTram(maTram);
							tram.setTenTram(tenTram);
							tram.setDinhDanh(dinhDanh);		
							tram.setLoaiTram(loaiTram);	
							tram.setCongSuatTram(cSuatTram);	
							tram.setMaCapDA(maCapDA);	
							tram.setMaCapDaRa(maCapDARa);	
							tram.setMaTo(maTo);	
							tram.setTinhTrang(tinhTrang);	
							tram.setHoSoPhu(hSPhu);	
							tram.setMaDonViQuanLy(maDVQL);
		
							listtramModel.add(tram);
						} catch (Exception e) {
						}
					} while (c.moveToNext());
				}
			}	
		} catch (Exception e) {
		}
		finally{
			if(c!=null)
			{
				c.close();
				c = null;
			}
		}

		return listtramModel;
	}
	
	public TramModel[] loadTramToArray() {
		Cursor c = null;
		
		try {
			if(dbHelper != null && dbHelper.getDb() != null)
			{
				c = dbHelper.getDb().rawQuery("select * from " + TramModel.TRAM_TABLE_NAME + " where " + COLUMN_MA_DVIQLY + " = ?", new String[]{SharedPreferencesData.getMaDonViQuanLy(MainApplication.getAppContext())});

				if (c != null && c.getCount() > 0) {
					TramModel[] tramArr = new TramModel[c.getCount()];
							
					int colId_MaDVQL = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[0]);
					int colId_Matram = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[1]);
					int colId_TenTram = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[2]);
					int colId_DinhDanh = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[3]);
					int colId_LoaiTram = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[4]);
					int colId_CSuatTram = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[5]);
					int colId_MaCapDA = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[6]);
					int colId_MaCapDARa = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[7]);
					int colId_MaTo = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[8]);
					int colId_TinhTrang = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[9]);
					int colId_HSPhu = c.getColumnIndex(TramModel.TRAM_FULL_PROJECTION[10]);

					c.moveToFirst();
					
					int i = 0;
					do {
						try {
							String maDVQL = c.getString(colId_MaDVQL);
							String maTram = c.getString(colId_Matram);
							String tenTram = c.getString(colId_TenTram);
							String dinhDanh = c.getString(colId_DinhDanh);		
							String loaiTram = c.getString(colId_LoaiTram);	
							int cSuatTram = c.getInt(colId_CSuatTram);	
							String maCapDA = c.getString(colId_MaCapDA);	
							String maCapDARa = c.getString(colId_MaCapDARa);	
							String maTo = c.getString(colId_MaTo);	
							int tinhTrang = c.getInt(colId_TinhTrang);	
							int hSPhu = c.getInt(colId_HSPhu);	

							TramModel tram = new TramModel();

							tram.setMaTram(maTram);
							tram.setTenTram(tenTram);
							tram.setDinhDanh(dinhDanh);		
							tram.setLoaiTram(loaiTram);	
							tram.setCongSuatTram(cSuatTram);	
							tram.setMaCapDA(maCapDA);	
							tram.setMaCapDaRa(maCapDARa);	
							tram.setMaTo(maTo);	
							tram.setTinhTrang(tinhTrang);	
							tram.setHoSoPhu(hSPhu);	
							tram.setMaDonViQuanLy(maDVQL);

							tramArr[i] = tram;
							i++;
//							listtramModel.add(tram);
						} catch (Exception e) {
						}
					} while (c.moveToNext());
					
					return tramArr;
				}
			}
		} catch (Exception e) {
		}
		finally{
			if(c!=null)
			{
				c.close();
				c = null;
			}
		}
		
		return null;
	}
}
